var crypto = require('crypto');

function md5(data) {
    return crypto.createHash('md5').update(data).digest('hex');
}

function en128(data, key) {
    var cipher = crypto.createCipher('aes-128-ecb', key);
    return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}

function de128(data, key) {
    var cipher = crypto.createDecipher('aes-128-ecb', key);
    return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}

function en256(data, key) {
    var cipher = crypto.createCipher('aes-256-ecb', key);
    return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}

function de256(data, key) {
    var cipher = crypto.createDecipher('aes-256-ecb', key);
    return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}

function en128md5(data, key) {
    var cipher = crypto.createCipher('aes-128-ecb', md5(key));
    return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}

function de128md5(data, key) {
    var cipher = crypto.createDecipher('aes-128-ecb', md5(key));
    return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}

function en256md5(data, key) {
    var cipher = crypto.createCipher('aes-256-ecb', md5(key));
    return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}

function de256md5(data, key) {
    var cipher = crypto.createDecipher('aes-256-ecb', md5(key));
    return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}

function en128vi(data, key) {
    var cipher = crypto.createCipheriv('aes-128-ecb', key, '');
    cipher.setAutoPadding(true);
    return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}

function de128vi(data, key) {
    var cipher = crypto.createDecipheriv('aes-128-ecb', key, '');
    cipher.setAutoPadding(true);
    return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}

var data = 'liuzy',
    key = '123';

console.log('en128\t=>\t' + en128(data, key));// b940e9df81cdeca66de4d6297a2f08af
console.log('de128\t=>\t' + de128(en128(data, key), key));// yes

console.log('en256\t=>\t' + en256(data, key));// c3287900482609282bd622f94d61d241
console.log('de256\t=>\t' + de256(en256(data, key), key));// yes

console.log('en128md5\t=>\t' + en128md5(data, key));// 6828223d5e4850d053b6b3566b60a586
console.log('de128md5\t=>\t' + de128md5(en128md5(data, key), key));// yes

console.log('en256md5\t=>\t' + en256md5(data, key));// 4330ba388a1cf9c9e391d02af9591e62
console.log('de256md5\t=>\t' + de256md5(en256md5(data, key), key));// yes

// console.log('en128vi\t=>\t' + en128vi(data, key));// error
